package com.zsc.edu.gateway.modules.iot.tsl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.iot.tsl.dto.EventDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Event;
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventLog;
import com.zsc.edu.gateway.modules.iot.tsl.query.EventQuery;
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventLogRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.EventService;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * @author zhuang
 */
@AllArgsConstructor
@RestController
@RequestMapping("api/rest/tsl/event")
public class EventController {

    private final EventService service;

    private final EventLogRepository eventLogRepository;


    /**
     * 创建事件
     *
     * @param dto 创建的事件
     */
    @PostMapping
    @PreAuthorize("hasAuthority('iot:event:create')")
    @OperationLogAnnotation(content = "'事件'", operationType = "新建")
    public Event create(@RequestBody EventDto dto) {
        return service.create(dto);
    }


    /**
     * 更新事件
     *
     * @return 更新后的事件
     */
    @PatchMapping("{id}")
    @PreAuthorize("hasAuthority('iot:event:update')")
    @OperationLogAnnotation(content = "'事件'", operationType = "更新")
    public Event update(@RequestBody EventDto dto, @PathVariable("id") Long id) {
        return service.update(dto, id);
    }


    /**
     * 查询事件
     *
     * @param query    事件的查询表单
     * @param page 分页参数
     * @return Page<Device> 事件分页数据
     */
    @GetMapping
    @PreAuthorize("hasAuthority('iot:event:query')")
    public Page<Event> page(Page<Event> page, EventQuery query) {
        return service.page(page, query.wrapper());
    }

    /**
     * 查询事件记录
     *
     * @param clientId 事件ClientId
     * @param page     分页参数
     * @return Page<Device> 事件分页数据
     */
    @GetMapping("/{clientId}/log")
    @PreAuthorize("hasAuthority('iot:event:query')")
    public IPage<EventLog> eventLogPage(Page<EventLog> page, @PathVariable("clientId") String clientId) {
        return eventLogRepository.selectPage(page, new LambdaQueryWrapper<EventLog>().eq(EventLog::getRecordId, clientId));
    }


    /**
     * 删除事件
     *
     * @param id 事件编号
     * @return 被删除的事件
     */
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('iot:event:delete')")
    @OperationLogAnnotation(content = "'事件'", operationType = "删除")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.delete(id);
    }

    /**
     * 事件
     * 查询详情
     *
     * @param id ID
     * @return 任务
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('iot:event:delete')")
    public Event detail(@PathVariable("id") Long id) {
        return service.detail(id);
    }

    /**
     * 更新事件状态
     *
     * @return 更新后的事件
     */
    @PatchMapping("/toggle/{id}")
    @PreAuthorize("hasAuthority('iot:event:update')")
    @OperationLogAnnotation(content = "'事件启用状态'", operationType = "更新")
    public Boolean updateEnabled(@PathVariable("id") Long id) {
        return service.toggle(id);
    }

}
